<template>
  <div class="login-container">
    <div class="login-left">
    </div>

    <div class="login-right">
      <div class="login-form">
        <h2 class="welcome">欢迎注册</h2>
        <p class="sub-title">智慧学习考核系统</p>
        
        <el-form
          ref="registerFormRef"
          :model="registerForm"
          :rules="registerRules"
          class="form"
        >
          <el-form-item prop="name">
            <el-input
              v-model="registerForm.name"
              placeholder="请输入您的姓名"
              :prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item prop="mobile">
            <el-input
              v-model="registerForm.mobile"
              placeholder="请输入您的手机号" type="number"
              :prefix-icon="Phone"
            />
          </el-form-item>
          
          <el-form-item prop="code">
            <el-input
                v-model="registerForm.code"
                placeholder="请输入验证码" type="number"
                :prefix-icon="Message"
              />
          </el-form-item>

          <!-- 滑块验证 -->
          <el-form-item prop="slideVerified">
            <slide-verify @success="onVerifySuccess" @failed="onVerifyFailed" />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入由数字、字母和特殊字符至少其中两种组成的8-15位密码"
              :prefix-icon="Lock"
            />
          </el-form-item>
          
          <el-button
            type="primary"
            class="register-button"
            :loading="loading"
            @click="handleRegister"
          >
            注册
          </el-button>
          
          <div class="login-link">
            <span>已有学员账号，</span>
            <el-link type="primary" @click="goToLogin">去登录 ></el-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import { User, Phone, Message, Lock, ArrowRight, CircleCheck } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { userApi } from '@/api/modules/user.js'
import SlideVerify from '@/components/SlideVerify.vue'
import { Decrypt ,Encrypt } from '@/utils/encryption';
import { validatePassword } from '@/utils/common.js'
// import { register, sendSmsCode } from '@/api/user'

const router = useRouter()
const registerFormRef = ref(null)
const loading = ref(false)
const isCodeSending = ref(false)
const countdown = ref(60)
const timer = ref(null)
const isSlideVerified = ref(false)
const showSlideDialog = ref(false)

const registerForm = reactive({
  name: '',
  mobile: '',
  code: '',
  password: '',
  slideVerified: false
})

const registerRules = {
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z\u4e00-\u9fa5]+( [a-zA-Z\u4e00-\u9fa5]+)*$/,
      message: '姓名只能包含字母或汉字，并且不能有非法字符',
      trigger: 'blur'
    }
  ],
  mobile: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码长度应为6位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { validator: validatePassword, trigger: 'blur' }
  ],
  slideVerified: [
    { required: true, message: '请完成滑块验证', trigger: 'change' }
  ]
}

const onVerifySuccess= async()=>{
  registerForm.slideVerified = true
  if (registerForm.mobile) {
    try {
      const data = await userApi.sms({
        mobile:registerForm.mobile,
        sms_type:'register'
      });
      ElMessage.success('发送验证码成功')
    } catch (error) {
      ElMessage.error('发送验证码失败，请检查输入的信息');
    }
  } else {
    ElMessage.warning('请先输入手机号')
  }
}

// 注册
const handleRegister = async () => {
  if (!registerFormRef.value) return
  if(!registerForm.slideVerified){
    ElMessage.error('请进行滑块验证');
    return
  }
  try {
    await registerFormRef.value.validate()
    loading.value = true
    const encryptedPassword = Encrypt(registerForm.password);
    const res = await userApi.studentSignUp({
      name:registerForm.name,
      mobile:registerForm.mobile,
      code:registerForm.code,
      password:encryptedPassword,
    })
    ElMessage.success('注册成功')
    setTimeout(function(){
      router.push('/login')
    },1000)
  } catch (error) {
    console.log(error)
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}

// 组件销毁时清除定时器
onBeforeUnmount(() => {
  if (timer.value) {
    clearInterval(timer.value)
  }
})
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  background: #fff;
  padding: 8px;
  
  .login-left {
    flex: 3;
    padding: 40px;
    background-image: url('@/assets/images/login-bg.png');
    background-size: cover;
    position: relative;
    
    .logo-title {
      display: flex;
      align-items: center;
      gap: 12px;
      
      .logo {
        width: 40px;
        height: 40px;
      }
      
      span {
        font-size: 24px;
        font-weight: 500;
        color: #1890FF;
      }
    }
    
    .login-bg {
      width: 80%;
      max-width: 600px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
  
  .login-right {
    flex: 2;
    // width: 500px;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .login-form {
      width: 360px;
      :deep(.el-input__inner){
        border-left: 1px solid #eee;
        padding-left: 10px;
      }
      .welcome {
        font-family: MiSans, MiSans;
        font-weight: 600;
        font-size: 36px;
        color: rgba(0,0,0,0.88);
        line-height: 44px;
        letter-spacing: 7px;
        text-align: center;
        font-style: normal;
        text-transform: none;
        margin-bottom: 8px;
      }
      
      .sub-title {
        font-family: MiSans, MiSans;
        font-weight: 500;
        font-size: 24px;
        color: rgba(0,0,0,0.65);
        line-height: 32px;
        letter-spacing: 6px;
        text-align: center;
        font-style: normal;
        text-transform: none;
        margin-bottom: 40px;
      }
      
      .form {
        .el-input {
          height: 40px;
          
          :deep(.el-input__inner) {
            height: 40px;
          }
        }
        
        .remember-forgot {
          display: flex;
          justify-content: space-between;
          align-items: center;
          text-align: right;
          margin: 16px 0 24px;
          
          .el-checkbox {
            color: #4E5969;
          }
        }
        
        .login-button {
          width: 100%;
          height: 40px;
          font-size: 16px;
          margin-bottom: 16px;
        }
        
        .register {
          font-family: MiSans, MiSans;
          font-weight: 400;
          font-size: 14px;
          text-align: right;
          :deep(.el-link__inner){
            color: rgba(0,0,0,0.65) !important;
            line-height: 22px;
            font-style: normal;
            text-transform: none;
          }
          
        }
      }
    }
  }
}

// 响应式布局
@media screen and (max-width: 768px) {
  .login-container {
    .login-left {
      display: none;
    }
    
    .login-right {
      width: 100%;
      padding: 20px;
      
      .login-form {
        width: 100%;
      }
    }
  }
}

.slide-verify {
  .verify-button {
    width: 100%;
    height: 40px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    cursor: pointer;
    transition: all 0.3s;
    
    &:hover {
      border-color: #409eff;
    }
    
    span {
      color: #606266;
      font-size: 14px;
    }
  }
  
  .verify-success {
    width: 100%;
    height: 40px;
    background: #f0f9eb;
    border: 1px solid #67c23a;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    
    span {
      color: #67c23a;
      font-size: 14px;
    }
    
    .el-icon {
      color: #67c23a;
    }
  }
}

.slide-container {
  display: flex;
  justify-content: center;
  padding: 20px 0;
}

.code-input {
  display: flex;
  gap: 10px;
  
  .el-input {
    flex: 1;
  }
  
  .code-button {
    width: 120px;
  }
}

.register-button {
  width: 100%;
  height: 40px;
  margin: 20px 0;
}

.login-link {
  text-align: right;
  font-size: 14px;
  color: #33A0FF;
}
</style>
